#include <stdio.h>
#include "CORBA_skel/collaboration.h"
#include "MIDataAccessor.h"
#include "MIApplication.h"
#include "MIUser.h"
#include "MIRole.h"
#include "dbAccessor.h"
#include "debug.h"

inline void test_MIUser(collaboration::MIUser* o)
{
  	o->username = CORBA::string_dup("usuario");
  	printf("%s\n",(char*)o->username);

  	o->password = CORBA::string_dup("clave");
  	printf("%s\n",(char*)o->password);

	collaboration::ListOfRoleNames* lr = new collaboration::ListOfRoleNames();
	
	lr->length(2);
	(*lr)[0] = CORBA::string_dup("r1");
	(*lr)[1] = CORBA::string_dup("r2");
	
	o->allowed_roles = *lr;

	delete lr;
	
	lr = &(o->allowed_roles);
	
	for (unsigned int i=0; i<lr->length(); i++)
	{
		printf("%s\n", (char*)(*lr)[i]);
	}
	
	o->default_role = CORBA::string_dup("role");
  	printf("%s\n", (char*)o->default_role);

  	o->actual_role = CORBA::string_dup("rolexxx");
	printf("%s\n", (char*)o->actual_role);
  	MIUser::is_online(o, true);
  	printf("%d\n", o->is_online);
  	o->is_admin = true;
  	printf("%d\n", o->is_admin);
  	o->last_time_updated = time(0);
  	printf("%ld\n", (long)o->last_time_updated);
  	o->identifier = 33;
  	printf("%ld\n", o->identifier);

	collaboration::ListOfComponents* lc = &(o->components);
	int len = lc->length();
	lc->length(len+1);
	collaboration::MIComponent* mc = new collaboration::MIComponent;
	mc->name = CORBA::string_dup("pepito");
	(*lc)[len] = *mc;
	
	o->components = *lc;

	if (MIUser::has_allowed_role(o, "role"))
	{
		printf("tiene rol\n");
	}

  	/*
  	CORBA::Boolean add_allowed_role(const char* role);
  	CORBA::Boolean remove_allowed_role(const char* role);
  	CORBA::Long get_component_permissions(const char* comp_name);
  	void set_component_permissions(const char* comp_name, CORBA::Long level);
	void add_component_permissions(const char* comp_name, CORBA::Long level);*/
}

inline void test_MIRole(collaboration::MIRole* o)
{

}

inline void test_MIApplication(collaboration::MIApplication* o)
{
	
}

inline void test_DataAccessor(MIDataAccessor_impl* o)
{
	CORBA::String_var app = CORBA::string_dup("App");
	CORBA::String_var role = CORBA::string_dup("prueba_role");
	CORBA::String_var role2 = CORBA::string_dup("role2");
	CORBA::String_var user = CORBA::string_dup("a");
	CORBA::String_var clave = CORBA::string_dup("clave");
	
	collaboration::ListOfApplications* l = o->applications();
	printf("%d\n", (int)l->length());
	for (unsigned int i=0; i<l->length(); i++)
	{
		printf("Aplicacion:  %s\n", (char*)(*l)[i].name);
		fflush(stdout);
	}

	o->add_application("prueba");
	
	collaboration::ListOfApplications* ll = o->applications();
	printf("%d\n", (int)ll->length());
	for (unsigned int i=0; i<ll->length(); i++)
	{
		printf("Aplicacion:  %s\n", (char*)(*ll)[i].name);
		fflush(stdout);
	}
	
	o->new_role(app, role);
	o->new_role(app, role2);
	o->new_user(app, user, clave, role, false);

	collaboration::ListOfUsers* u = o->users_info(app);
	
	if (u != NULL)
	{
		for (unsigned int i=0; i<u->length(); i++)
		{
			printf("%s, %s\n", (char*)(*u)[i].username, (char*)(*u)[i].password);
		}
	
		collaboration::ListOfRoles* r = o->roles_info(app);
	
		for (unsigned int i=0; i<r->length(); i++)
		{
			printf("%s\n", (char*)(*r)[i].role_name);
		}
	
		if (o->login(app, user, clave))
		{
			printf("Usuario logueado\n");
		}
	
		if (o->connect_user(app, user))
		{
			printf("Usuario conectado\n");
		}
		
		collaboration::ListOfUsersAndRoles* cu = o->connected_users(app);
		
		for (unsigned int i=0; i<cu->length(); i++)
		{
			printf("%s, %s\n", (char*)(*cu)[i][0], (char*)(*cu)[i][1]);
		}
		
		o->change_connected_user_role(app, user, role2);
		
		printf("%s    ", o->user_actual_role(app, user));
		
		o->new_user_allowed_role(app, user, role2);
		o->change_connected_user_role(app, user, role2);
		
		printf("%s\n", o->user_actual_role(app, user));
		
		o->disconnect_user(app, user);
		printf("Usuario desconectado\n");
	
		o->update_user_time(app, user);
	}
	else printf("no hay usuarios\n");
	
	
	
	o->commit(); // REVISAR INSERCION DE CLAVES USADA
	delete u;
	
	/*
  	collaboration::ListOfUsers* connected_users_info(const char* app);
  	collaboration::MIRole_ptr role_info(const char* app, const char* role_name);
  	collaboration::ListOfUserNames* role_users(const char* app, const char* role);
  	collaboration::ListOfUserNames* non_updated_users(const char* app);
  	collaboration::ListOfRoleNames* application_roles(const char* app);
  	collaboration::ListOfUserNames* application_users(const char* app);
  	collaboration::ListOfComponentNames* application_components(const char* app);
  	collaboration::ListOfRoles* allowed_roles(const char* app, const char* user);
  	collaboration::ListOfRoleNames* allowed_role_names(const char* app, const char* user);
  	CORBA::Long get_user_component_permissions(const char* app, const char* user, const char* comp);
  	CORBA::Boolean set_user_component_permissions(const char* app, const char* user, const char* comp, CORBA::Long level);
  	CORBA::Long get_role_component_permissions(const char* app, const char* role, const char* comp);
  	CORBA::Boolean set_role_component_permissions(const char* app, const char* role, const char* comp, CORBA::Long level);
  	CORBA::Boolean new_user_allowed_role(const char* app, const char* user, const char* role);

  	CORBA::Boolean remove_allowed_user_role(const char* app, const char* user, const char* role);
  	CORBA::Boolean remove_user(const char* app, const char* user);
  	CORBA::Boolean remove_role(const char* app, const char* role);
  	collaboration::ListOfApplicationNames* get_applications_names();
  	collaboration::MIApplication_ptr get_application(const char* name);
  	*/
}

void test_all()
{
	//DATA ACCESSOR
	MIDataAccessor_impl* o1 = new MIDataAccessor_impl();
	test_DataAccessor(o1);
	
	//MIUser
	collaboration::MIUser* o3 = new collaboration::MIUser;
	test_MIUser(o3);
	
	delete o1;
	delete o3;
}
